<?php
# Installs SQL files contained in ?froms's Models-dir to ?site's .db-file
error_reporting(E_ALL);
ini_set('display_errors', true);

header('Content-type: text/plain');

define('DOCROOT', realpath(dirname(__FILE__) . '/../') . '/');
define('WEBROOT', str_replace('Utils/', '', str_replace('//', '/', dirname($_SERVER['SCRIPT_NAME']) . '/')));

include DOCROOT . 'Core/DB.php';

# Make sure all params are set
$site = isset($_GET['site']) ? basename($_GET['site']) : false;
$sitePath = file_exists(DOCROOT . "Sites/$site/$site.info") ? DOCROOT . "Sites/$site/$site.info" : false;

$from = isset($_GET['from']) ? basename($_GET['from']) : false;
$fromPath = file_exists(DOCROOT . "Sites/$from/$from.info") ? DOCROOT . "Sites/$from/$from.info" : false;

if ($sitePath and $fromPath) {
	$db = new DB(DOCROOT . "Sites/$site/$site.db");

	foreach (glob(DOCROOT . "Sites/$from/Models/*.sql") as $file) {
		$qry = file_get_contents($file);
		echo "Running: $file\n";

		$db->qry($qry);
	}

	echo 'DONE!';
}
else {
	echo 'Set ?site and ?from';
}
